package part18;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * @author wcj
 * @Date 2021/6/3 17:55
 * @Version 1.0
 */
public class Part1813 {

    static String file = "rtest.dat";

    public static void display() throws IOException {
        RandomAccessFile r = new RandomAccessFile(file, "r");
        for (int i = 0; i < 7; i++) {
            System.out.println(r.readDouble());
        }
        System.out.println(r.readUTF());
    }

    /**
     * RandomAccessFile的使用：其功能等于是结合了DataOutputStream和DataInputStream，但是是完全独立的
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        RandomAccessFile rw = new RandomAccessFile(file, "rw");
        for (int i = 0; i < 7; i++) {
            rw.writeDouble(i * 1.414);
        }
        rw.writeUTF("this is end of file");
        rw.close();
        display();
        rw = new RandomAccessFile(file, "rw");
        rw.seek(5 * 8);
        rw.writeDouble(87.58534);
        rw.close();
        display();
    }
}
